//
//  QLVisitorView.swift
//  WeiBo
//
//  Created by 黄跃奇 on 16/9/20.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

//MARK: - 1. 定义代理协议
protocol QLVisitorViewDelegate: NSObjectProtocol {
    func didSelectedLogin()
}

//访客视图
class QLVisitorView: UIView {
    
    //MARK: - 1.(闭包)定义点击登录和注册按钮的闭包
    var loginClosure: (()->())?
    
    //MARK: - 2. 定义代理对象
    weak var delegate: QLVisitorViewDelegate?
    

    //MARK: - 懒加载控件
    //1. 旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //2. 罩层
    private lazy var maskImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //3. 主页
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //4. 消息视图
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //多行
        label.numberOfLines = 0
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //居中显示
        label.textAlignment = .center
        return label
    }()
    //5. 登录按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //添加按钮的点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //去除点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    //6. 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //添加按钮的点击事件
        button.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        //去除点击的高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //实现按钮的点击事件
    @objc func loginAction() {
        
        print("我是访客视图点击的操作")
        //MARK: - 2.(闭包)执行点击登录和注册按钮的闭包
        loginClosure?()
        
        //MARK: - 3. 通过代理对象执行代理方法
        delegate?.didSelectedLogin()
        
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        //1. 设置背景颜色
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        //2. 添加到父视图
        addSubview(cycleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //3. 设置约束
        cycleImageView .snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView .snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        iconImageView .snp_makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        messageLabel .snp_makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp_bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        loginButton .snp_makeConstraints { (make) in
            make.leading.equalTo(messageLabel.snp_leading)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 35))
        }
        registerButton .snp_makeConstraints { (make) in
            make.trailing.equalTo(messageLabel.snp_trailing)
            make.top.equalTo(loginButton.snp_top)
            make.size.equalTo(loginButton)
        }
        
        
        //使用系统自动布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        
//        //设置布局方式
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
    }
    
    //修饰访客视图信息
    func updataVisitorInfo(message: String?, imageName: String?) {
        
        if let msg = message, let imgName = imageName {
            //代码执行到此表示是消息、发现、我的
            messageLabel.text = msg
            iconImageView.image = UIImage(named: imgName)
            cycleImageView.isHidden = true
        } else {
            //代码执行到此表示是首页
            //需要开启旋转动画
            startAnimation()
            
        }
    }

    //开启旋转动画
    private func startAnimation() {
        
        //创建核心动画对象
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //执行到的目的地
        anim.toValue = 2 * M_PI
        //动画时长
        anim.duration = 15
        //动画执行次数
        anim.repeatCount = MAXFLOAT
        //失去焦点让其不释放，解决切换视图动画停止的问题
        anim.isRemovedOnCompletion = false
        cycleImageView.layer.add(anim, forKey: nil)
    }
    
    
    
}














